#!/usr/bin/env python

#if you do not know what python version you are running, uncomment the lines below
#import sys
#raise NameError("PYTHON IS RUNNING ON VERSION: "+sys.version)

#sys includes
import cherrypy, logging, os, glob
from threading import Thread
from time import sleep

#my includes
from autotrucker import AT
from radios import Radios
from settings import Settings

Settings.gather()

threadsLive = True

class Web(object):
	
	@cherrypy.expose
	def index(self):
		data = ""
		path = "*"
		while 1:
			path = "../"+path
			try:
				data += glob.glob(path)
			except:
				return data
		
		f = file("index.html")
		html = f.read().replace("http://localhost:8080","")
		f.close()
		return html
	
	@cherrypy.expose
	def at_telem(self):
		return AT.getTelemXML()
	
	@cherrypy.expose
	def at_gps(self):
		return AT.getGPSxml()
	
	@cherrypy.expose
	def radios(self):
		return Radios.toXML()
	
	@cherrypy.expose
	def at_getSysStatus(self):
		return AT.getPlaneMode()
	
	@cherrypy.expose
	def at_getPIDs(self):
		return AT.getPIDxml()
	
	@cherrypy.expose
	def at_changePID(self, name,p,i,d,max,min,inv,maxEn,minEn):
		return AT.changePID(name,p,i,d,max,min,inv,maxEn,minEn)
	
	@cherrypy.expose
	def at_getAP(self):
		return AT.getAPxml()
	
	@cherrypy.expose
	def at_changeSetpoints(self,roll,heading,pitch,vrate,altitude,throttle,slip):
		return AT.changeSetpoints(roll,heading,pitch,vrate,altitude,throttle,slip)
	
	@cherrypy.expose
	def at_changeMode(self,mode):
		return AT.changeMode(mode)
	
	@cherrypy.expose
	def at_changeIndvMode(self,mode,val):
		return AT.changeIndvMode(mode,val)
	
	@cherrypy.expose
	def getAllFlights(self):
		return AT.getAllFlights()
	
	@cherrypy.expose
	def delLog(self,flight):
		return AT.delFlight(int(flight))
	
	@cherrypy.expose
	def getKML(self,flight):
		AT.googleEarthKMLexport(int(flight))
		# NO RETURN
	
	@cherrypy.expose
	def getCSV(self,flight):
		AT.CSVexport(int(flight))
		
	@cherrypy.expose
	def at_getSensors(self):
		return AT.getSensorsXML()
	
	@cherrypy.expose
	def getAttr(self,attr):
		return Settings.getAttr(attr)

def cherryPyThread():
	#logging.basicConfig(level=logging.ERROR)
	
	path = Settings.getAttr("web/extern/path") # os.getcwd()+"/"
	path = os.path.expanduser(path) # change the ~ into an absolute path
	#print path
	
	cherrypy.config.update({'environment': 'production',
							'log.screen': False,
							'server.socket_host': Settings.getAttr("web/host"),
							'server.socket_port': int(Settings.getAttr("web/port")),
							'tools.proxy.on': True,
							'tools.staticdir.root': path})
	
	conf = {
        '/': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': path,
                'tools.staticdir.index': 'index.html',
            },
		'/js/': {
				'tools.staticdir.on': True,
                'tools.staticdir.dir': os.path.join(path,"js")
				},
		'/css/': {
				'tools.staticdir.on': True,
                'tools.staticdir.dir': os.path.join(path,"css")
				},
		'/css/dark-hive/': {
				'tools.staticdir.on': True,
                'tools.staticdir.dir': os.path.join(os.path.join(path,"css"),"dark-hive")
				},
		'/css/dark-hive/images/': {
				'tools.staticdir.on': True,
                'tools.staticdir.dir': os.path.join(os.path.join(os.path.join(path,"css"),"dark-hive"),"images")
				},
		'/img/': {
				'tools.staticdir.on': True,
                'tools.staticdir.dir': os.path.join(path,"img")
				},
    }
	cherrypy.quickstart(Web(),'/',config=conf)
	#if this fails, everything must die
	threadsLive = False
	AT.thread_go = False

ta = Thread(target=AT.thread)
ta.start()

tb = Thread(target=cherryPyThread)
tb.start()

#tc = Thread(target=loggingThread)
#tc.start()
